/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-11-21 17:45:13
 * @LastEditors: huangtianyang
 * @LastEditTime: 2024-01-18 17:00:01
 * @FilePath: \cxl-driving-training\src\views\trainingGround\subjectTwo\index.ts
 */
import { BaseData, BaseMethod } from '@/utils/basc-data'
import { tableConfig } from './config'
import router from '@/router'
import { importSiteTwo, siteDisable, trainSiteList, trainSiteListInterface } from '@/apis/trainSite'
import { ElMessage } from 'element-plus'
import { CreateXMLHttpRequest } from '@/utils'

export default class SubjectTwoData extends BaseData implements BaseMethod {
    searchData = {
        name: '',
        addressDetail: '',
        siteType: '',
        ownFlag: '',
        status: ''
    }
    constructor() {
        super(tableConfig)
        this.getData()
    }
    getData = (arg?: any) => {
        const params: trainSiteListInterface = {
            current: this.pageData.pagination.pageNo,
            size: this.pageData.pagination.pageSize,
            siteCategory: 2
        }

        if (this.searchData.name) {
            params.name = this.searchData.name
        }
        if (this.searchData.addressDetail) {
            params.addressDetail = this.searchData.addressDetail
        }
        if (this.searchData.siteType !== '' && this.searchData.siteType !== '-1') {
            params.siteType = Number(this.searchData.siteType)
        }
        if (this.searchData.ownFlag !== '' && this.searchData.ownFlag !== '-1') {
            params.ownFlag = Number(this.searchData.ownFlag)
        }
        if (this.searchData.status !== '' && this.searchData.status !== '-1') {
            params.disable = Number(this.searchData.status)
        }

        trainSiteList(params).then(res => {
            if (Number(res.data.code) === 200) {
                this.pageData.table.data.length = 0
                this.pageData.pagination.total = res.data.result.total
                const arr: any[] = res.data.result.records
                arr.forEach((element: any) => {
                    element.status = Number(element.disable) === 0 ? true : false
                    this.pageData.table.data.push(element)
                })
            }
        })
    }
    onSearch = (arg?: any) => {
        console.log(arg)
        this.searchData = arg
        this.getData()
    }
    onReset = (arg?: any) => {
        this.searchData = {
            name: '',
            addressDetail: '',
            siteType: '-1',
            ownFlag: '-1',
            status: '-1'
        }
        this.getData()
    }
    onPaginationChange = (arg?: any) => {
        this.pageData.pagination.pageNo = arg
        this.getData()
    }
    onPaginationSize = (arg?: any) => {
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = arg
        this.getData()
    }
    onOperation = (arg?: any) => {
        const { type, data } = arg
        console.log(type)
        if (type === 'create') {
            router.push({ path: '/subjectTwo/operation', query: { type: 'create' } })
        } else if (type === 'edit' || type === 'look') {
            router.push({ path: '/subjectTwo/operation', query: { type, id: data.id } })
        } else if (type === 'import') {
            console.log(data[0])
            importSiteTwo(data[0]).then(res => {
                if (Number(res.data.code) === 200) {
                    ElMessage.success('上传成功')
                    this.getData()
                }
            })
        } else if (type === 'download') {
            //科二训练场：trainSiteTwo, 科三训练场：trainSiteThree, 驾校教练：schoolCoach;
            const url = import.meta.env.VITE_HOST + '/api/system/template/download/trainSiteTwo'

            CreateXMLHttpRequest(url, '科目二训练场模板')
        } else if (type === 'switch') {
            siteDisable(data.status ? 0 : 1, data.id).then(res => {
                if (Number(res.data.code) === 200) {
                    ElMessage.success('修改成功')
                    this.getData()
                }
            })
        }
    }
}
